package com.creative.accountsdk.net.account;

import android.util.Log;

import com.creative.accountsdk.UmsChannel;
import com.creative.accountsdk.UserData;
import com.creative.accountsdk.bean.ClassInfo;
import com.creative.accountsdk.bean.CourseInfo;
import com.creative.accountsdk.bean.StudentInfo;
import com.creative.accountsdk.net.Protocol;
import com.creative.accountsdk.net.XParams;
import com.loopj.android.http.RequestParams;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 班级详情信息
 *
 *
 * @author  David
 * @version 1.0
 * @created 2015-11-25
 */
public class ClassDetailProtocol extends Protocol {

    public JSONObject           returnMap;
    public ClassInfo            classInfor ;
    public String               courseStr="";
    public List<StudentInfo>     studentList;

     UserData  userData;
    long   classid ;
    public ClassDetailProtocol(long  classid){
        userData = UserData.getInstance();
        this.classid = classid;
    }
    @Override
    public RequestParams getParams() {
        return new XParams()
                .put("uid", userData.getUid())
                .put("classid",classid)
                .put(UmsChannel.PARAM_NAME, UmsChannel.getChannel())
                .getParams();
    }

    @Override
    public String getUrl() {
        long uid = userData.getUid();
        return getUrlPrefix()+"class/t/"+uid+"/"+classid;
    }

    @Override
    protected void parseReturnMap(JSONObject returnMap) {
        this.returnMap = returnMap;
            Log.e("Tag", "返回的班级详情信息.." + returnMap.toString());
            try{

                //班级信息
                if(returnMap.has("class")){
                    ClassInfo classInf =  new ClassInfo();
                    classInfor =  classInf.setJsonTovo(((JSONObject)returnMap.optJSONObject("class")));
                }
                //课程信息
                if(returnMap.has("courses")){
                   /* JSONArray  courseArray = returnMap.optJSONArray("courses");
                    if(null!=courseArray){
                        CourseInfo  courseInfo = new CourseInfo();
                        courseList = new ArrayList<>();
                        for(int i=0;i<courseArray.length();i++){
                            CourseInfo infor = ((CourseInfo)courseInfo.clone()).setJsonTovo((JSONObject)courseArray.opt(i));
                            courseList.add(infor);
                        }
                    }*/
                    courseStr = returnMap.optString("courses");;
                }
                //学生信息
                if(returnMap.has("students")){
                    JSONArray  studentsArray = returnMap.optJSONArray("students");
                    if(null!=studentsArray){
                        StudentInfo  studentInfo = new StudentInfo();
                        studentList = new ArrayList<>();
                        for(int i=0;i<studentsArray.length();i++){
                            StudentInfo infor = ((StudentInfo)studentInfo.clone()).setJsonTovo((JSONObject)studentsArray.opt(i));
                            studentList.add(infor);
                        }
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }

    }

}
